
//index.js
const app = getApp()
//var ble = require('../../common/ble')
let cmd = require('../../common/cmd')
let pay = require('../../common/pay')
let unit = require('../../common/unit')

Page({
  onShareAppMessage() {
    return {
      title: 'radio',
      path: 'page/component/pages/radio/radio'
    }
  },
  data: {
    items: [
      { value: '1', name: '0.5小时 ￥0.01', index: 0, tm: 0.5, checked: 'true' },
      { value: '2', name: '1.0小时 ￥0.02', index: 1, tm: 1.0 },
      { value: '3', name: '1.5小时 ￥0.03', index: 2, tm: 1.5 },
      { value: '4', name: '2.0小时 ￥0.04', index: 3, tm: 2.0 },
      { value: '5', name: '2.5小时 ￥0.05', index: 4, tm: 2.5 },
      { value: '6', name: '3.0小时 ￥0.06', index: 5, tm: 3.0 },
      { value: '7', name: '3.5小时 ￥0.07', index: 6, tm: 3.5 },
      { value: '8', name: '4.0小时 ￥0.08', index: 7, tm: 4.0 },
      { value: '9', name: '4.5小时 ￥0.09', index: 8, tm: 4.5 },
      { value: '10', name: '5.0小时 ￥0.10', index: 9, tm: 5.0 },
    ],
    time: "00:30",//购买时长
    balanceTime: "00:30",//剩余时间
    tm: 30,//剩余分钟
    pr: '10',//显示金额
    isServer: false,//true
    isFf:false,//是否正在付费 主要是按钮置灰
    isZt:false,//是否正在停止操作 主要是按钮置灰
    //decTime: "00:30",
    //stopTitle: "暂停服务",
    isStop: false,//是否暂停
    totalFee:1 //付款金额
  },

  onLoad: function (options) {
    // 在onLoad事件中提取q参数并自行decodeURIComponent一次
    //console.log("app=",app)
    // 即可获取原二维码的完整内容

    if (options.q) {
      const qrCodeContent = decodeURIComponent(options.q)
      console.log("二维码的内容:", qrCodeContent)
      const meetingID = qrCodeContent.split('&')
      console.log("从二维码中解析的id为：", meetingID[1], meetingID[2])
      this.data.bleMac = meetingID[1].toUpperCase()
      this.data.devId = meetingID[2].toUpperCase()
      cmd.initNet(this.data.bleMac, this.data.devId, this)
    } else if (options.devId) {
      console.log("传入参数:", options)
      this.data.bleMac = options.bleMac.toUpperCase()
      this.data.bleName = options.devId.toUpperCase()
      this.data.devId =parseInt(this.data.bleName,16) 
      cmd.initNet(this.data.bleMac, this.data.devId, this,this.data.bleName)
    } else {
      console.log("没有二维码传入参数")
    }
  },
  radioChange: function (e) {
    const items = this.data.items;
    app.globalData.tm = items[e.detail.value].tm;
    app.globalData.pr = items[e.detail.value].value;
    console.log('选择值为：', app.globalData.tm, app.globalData.pr);

    /*const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }

    this.setData({
      items
    })*/
  },
  //禁止快速重复点击按钮,timeCount为禁止时间(毫秒)
  disabledBt :function (timeCount){
    let that=this;
    if (!timeCount){
      timeCount=1000;
    }
    that.setData({
      isFf:true,//是否正在付费 主要是按钮置灰
      isZt:true,//是否正在停止操作 主要是按钮置灰
      })
      setTimeout(function(){
        that.setData({
          isFf:false,//是否正在付费 主要是按钮置灰
          isZt:false,//是否正在停止操作 主要是按钮置灰
          })
        },timeCount)

  },
  btClick: function () {
    
    console.log("提交订单")
    this.disabledBt(1000)//禁止按钮3秒
    //cmd.startServer(this.data.tm, false)//开始服务
    pay.postOrder(this)//开始支付
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.data.tm = unit.timeToMinutes(e.detail.value);
    console.log("服务时间:", this.data.tm)
    //(app.globalData.tm / 30).toFixed(0)
    this.data.totalFee = (this.data.tm / 30).toFixed(0);

    this.setData({
      time: e.detail.value,
      pr: (this.data.tm / 3).toFixed(0)
      //pr: (this.data.totalFee / 100).toFixed(2)
    })
  },
  btClose: function () {
    let tm=this.data.tm
    let that=this
    wx.showModal({
      title: '停止吸氧服务',
      content: '吸氧服务停止后将不会再次启用,剩余的吸氧时间将作废!!!',
      success(res) {
        if (res.confirm) {
          that.disabledBt(1000)//禁止按钮3秒
          cmd.startServer(0, false)

          //console.log('用户点击确定')
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },
  btStop: function () {
   // this.data.isStop = !(this.data.isStop)
   this.disabledBt(1000)//禁止按钮4秒 
   this.setData({
      isStop: !this.data.isStop
    })
    //let btTitle = "暂停服务";
    if (this.data.isStop) {
      //btTitle = "继续服务"
      cmd.startServer(0, true)
     } else {
      cmd.startServer(this.data.tm, false)
    }
  }
})
